package both.map.mapView;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import both.map.TerrainType;

public class MapBuilder extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;

	private TerrainType selected;
	private JRadioButton grass, dirt, cobble, water, stoneFloor, stoneWall;
	private JLabel picture;
	private ButtonGroup group;
	private JPanel buttonPanel, examplePanel;

	public MapBuilder() {

		setBorder(BorderFactory.createLineBorder(Color.CYAN));

		selected = TerrainType.grass;

		buttonPanel = new JPanel();
		BoxLayout buttonLayout = new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS);
		buttonPanel.setLayout(buttonLayout);

		grass = new JRadioButton("Grass");
		dirt = new JRadioButton("Dirt");
		cobble = new JRadioButton("Cobble");
		water = new JRadioButton("Water");
		stoneFloor = new JRadioButton("Stone Floor");
		stoneWall = new JRadioButton("Stone Wall");

		group = new ButtonGroup();

		group.add(grass);
		group.add(dirt);
		group.add(cobble);
		group.add(water);
		group.add(stoneFloor);
		group.add(stoneWall);

		grass.setSelected(true);

		grass.addActionListener(this);
		dirt.addActionListener(this);
		cobble.addActionListener(this);
		water.addActionListener(this);
		stoneFloor.addActionListener(this);
		stoneWall.addActionListener(this);

		buttonPanel.add(grass);
		buttonPanel.add(dirt);
		buttonPanel.add(cobble);
		buttonPanel.add(water);
		buttonPanel.add(stoneFloor);
		buttonPanel.add(stoneWall);

		examplePanel = new JPanel();

		picture = new JLabel(createImageIcon("images/grass.png"));

		examplePanel.add(picture);

		add(buttonPanel);
		add(examplePanel);
	}

	public TerrainType getSelected() {
		return selected;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(grass)) {
			selected = TerrainType.grass;
		} else if (e.getSource().equals(dirt)) {
			selected = TerrainType.dirt;
		} else if (e.getSource().equals(cobble)) {
			selected = TerrainType.cobble;
		} else if (e.getSource().equals(water)) {
			selected = TerrainType.water;
		} else if (e.getSource().equals(stoneFloor)) {
			selected = TerrainType.stoneFloor;
		} else if (e.getSource().equals(stoneWall)) {
			selected = TerrainType.stoneWall;
		}
		picture.setIcon(new ImageIcon(selected.getDefaultImageFile()));
	}

	private ImageIcon createImageIcon(String path) {
		BufferedImage img = null;		
		try {
			img = ImageIO.read(new File(path));
		} catch (IOException e) {
			e.printStackTrace();
		}	
		return new ImageIcon(img);
	}
}
